package com.baizhi.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baizhi.dao.CmfzGuruDao;
import com.baizhi.entity.CmfzGuru;
import com.baizhi.service.CmfzGuruService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("guru")
public class CmfzGuruController {
    @Autowired
    private CmfzGuruService cgs;

    //显示所有上师
    @RequestMapping("allGuruPage")
    public Map allGuruPage(@RequestParam(defaultValue = "1",value = "page") int pageNo,
                           @RequestParam(defaultValue = "3",value = "limit") int pageSize){
        Map<String,Object> map=new HashMap<>();
        Page<CmfzGuru> page = cgs.guruListByPage(pageNo, pageSize);
        map.put("code",0);
        map.put("msg","");
        map.put("data",page.getRecords());
        map.put("count",page.getTotal());
        return map;

    }

    //添加上师

    @RequestMapping("addGuru")
    public Boolean addGuru(CmfzGuru cmfzGuru){
        System.out.println(cmfzGuru);
        try {
            cgs.insert(cmfzGuru);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    //删除上师
    @RequestMapping("delGuru")
    public Boolean delGuru(int id){

        try {
            cgs.delete(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }


    //查询一个上师 修改必须操作
    @RequestMapping("selOneGuru")

    public CmfzGuru selOneGuru(int id){

        return cgs.selectOne(id);

    }
    //修改上师
    @RequestMapping("updGuru")
    public Boolean updGuru(CmfzGuru cmfzGuru){

        try {
            cgs.update(cmfzGuru);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    //上传
    @RequestMapping("upload")
    public Map upload(MultipartFile file, HttpServletRequest request) throws IOException {
        //在文件名的前面添加上uuid避免文件名被覆盖
        String newName= UUID.randomUUID().toString()+ file.getOriginalFilename();

        //把文件复制到这个目录里面。
        file.transferTo(new File("D:/img/"+newName));

        //响应信息。code=0表示上传成功，src是图片上传后新的名字
        Map map=new HashMap();
        map.put("code",0);
        map.put("src",newName);
        return map;
    }

    //所有上师不分页
    @Autowired
    private CmfzGuruDao cgd;
    @RequestMapping("allGuru")
    public List<CmfzGuru> allGuru(){
        List<CmfzGuru> cmfzGurus = cgd.selectList(null);
        return cmfzGurus;
    }

    //将上师所有的数据导出Excel
    @RequestMapping("guruToExcel")
    public String guruToExcel(HttpServletResponse response) throws IOException {
        String s="gurus.xls";
        response.reset();
        response.setHeader("Content-Disposition","attachment;fileName="+ URLEncoder.encode(s,"utf-8"));
        response.setContentType("application/vnd.ms-excel;charsert:utf-8");
        response.setHeader("Pragma","no-cache");
        response.setHeader("Cacha-Control","no-cache");
        response.setDateHeader("Expires",0);
        List<CmfzGuru> cmfzGurus=cgd.selectList(null);
        for (CmfzGuru gurus : cmfzGurus) {
            System.out.println(gurus);
        }
        ExportParams exportParams = new ExportParams("标题", "gurus");
        Workbook sheets = ExcelExportUtil.exportExcel(exportParams, CmfzGuru.class, cmfzGurus);
        OutputStream outputStream = response.getOutputStream();
        BufferedOutputStream stream = new BufferedOutputStream(outputStream);
        sheets.write(stream);
        stream.flush();
        stream.close();
        outputStream.close();

        return "success";

    }

    //导入，将Excel文件导入为数据库中的数据
    /**
     * 导入
     *
     */
    @RequestMapping("excelToJava")
    public Map excelToJava(MultipartFile file) throws Exception {
        //流读取文件



        InputStream inputStream = file.getInputStream();
       // HSSFWorkbook sheets = new HSSFWorkbook(inputStream);
        Map<String,Object> map=new HashMap<>();
        ImportParams importParams = new ImportParams();
        importParams.setTitleRows(1);
        importParams.setHeadRows(1);
        List<CmfzGuru> list=ExcelImportUtil.importExcel(inputStream,CmfzGuru.class,importParams);
        for (CmfzGuru cmfzGuru : list) {
            cgd.insert(cmfzGuru);
        }
        map.put("code",0);
        return map;

    }


}
